package cn.sumpu.app.coolsk.biz.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import cn.sumpu.app.coolsk.biz.TripBiz;
import cn.sumpu.app.coolsk.dao.TripDao;
import cn.sumpu.app.coolsk.domain.Trip;
import cn.sumpu.app.coolsk.domain.TripDay;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.page.PageModel;
import cn.sumpu.toolkit.common.util.Common;

public class TripBizImpl implements TripBiz{
	
	@Autowired
	private TripDao tripDao;

	public void setTripDao(TripDao tripDao) {
		this.tripDao = tripDao;
	}

	
	@Override
	public void insterTrip(Trip trip)throws AppBusinessException { 
		
		tripDao.insterTrip(trip);
	}
	@Override
	public void updateTrip(Trip trip)throws AppBusinessException { 
		
		tripDao.updateTrip(trip);
	}
	@Override
	public void deleteAllDay(String tripId)throws AppBusinessException { 
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tripId", tripId);
		tripDao.deleteAllDay(map);
	}
	
	@Override
	public void insterTripDay(TripDay tripDay)throws AppBusinessException {
		
		
		tripDao.insterTripDay(tripDay);
	}
	
	@Override
	public PageModel getTripListByMid(Integer page,Integer pagesize, String memberId)throws AppBusinessException {
		
		Map<String, Object> map = new HashMap<String, Object>();
		int size = pagesize;
		map.put("startId", (page - 1) * size);
		map.put("pageSize", size);
		map.put("member", memberId);
		int count = tripDao.getTripCountByMid(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<Trip> tripList = tripDao.getTripListByMid(map);
		for(Trip tripOne :tripList){  
			Map<String, Object> mapDay = new HashMap<String, Object>();
			mapDay.put("tripId", tripOne.getId());  
			List<TripDay> tripdayList =tripDao.selectTripDayListBytid(mapDay);
			tripOne.setMyTripDayList(tripdayList);
		}
		pm.setList(tripList);
		pm.setPageSize(size);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + size - 1) / size);
		return pm;
	}

	/**
	 * author BruceZhang
	 * @param pageSize		每页条数
	 * @param page			页码
	 * @return	行程管理
	 * @throws AppBusinessException
	 */
	@Override
	public PageModel getTripList(String title,Integer pageSize, Integer page)
			throws AppBusinessException {
		if(page==null || page<1){ 
			page = 1;
		}
		if(pageSize==null || pageSize<1){
			pageSize = 10;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageSize", pageSize);
		map.put("startId", (page - 1) * pageSize);
		if (!Common.empty(title)) {
            map.put("title", "%" + title + "%");
        }
		List<Trip> tripList = tripDao.getTripList(map);
		Integer count = tripDao.getTripCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(tripList);
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1) / pageSize);
		return pm;
	}


	@Override
	public int getTripCount() throws AppBusinessException {
	    Map<String, Object> map=new HashMap<String, Object>();
		return tripDao.getTripCount(map);
	}
	@Override
	public Trip selectTripById(String id)throws AppBusinessException {
		
		Trip trip= tripDao.selectTripById(id);
		Map<String, Object> mapDay = new HashMap<String, Object>();
		mapDay.put("tripId", id);  
		
		for(int i=1;i<=trip.getDays();i++){    
			if(i==1){
				mapDay.put("day", i); 
				List<TripDay> tripdayList1 =tripDao.selectTripDayListBytid(mapDay);
				trip.setMyTripDayList1(tripdayList1);
			}
			if(i==2){
				mapDay.put("day", i); 
				List<TripDay> tripdayList2 =tripDao.selectTripDayListBytid(mapDay);
				trip.setMyTripDayList2(tripdayList2);
			}
			if(i==3){
				mapDay.put("day", i); 
				List<TripDay> tripdayList3 =tripDao.selectTripDayListBytid(mapDay);
				trip.setMyTripDayList3(tripdayList3);
			}
			if(i==4){
				mapDay.put("day", i); 
				List<TripDay> tripdayList4=tripDao.selectTripDayListBytid(mapDay);
				trip.setMyTripDayList4(tripdayList4);
			}
			if(i==5){
				mapDay.put("day", i); 
				List<TripDay> tripdayList5 =tripDao.selectTripDayListBytid(mapDay);
				trip.setMyTripDayList5(tripdayList5);
			}
			if(i==6){
				mapDay.put("day", i); 
				List<TripDay> tripdayList6 =tripDao.selectTripDayListBytid(mapDay);
				trip.setMyTripDayList6(tripdayList6);
			}
			if(i==7){
				mapDay.put("day", i); 
				List<TripDay> tripdayList7 =tripDao.selectTripDayListBytid(mapDay);
				trip.setMyTripDayList7(tripdayList7);
			}
			
		}
		 //查询多少天的循环
		//List<TripDay> dayList =tripDao.selectDayListBytid(mapDay);
		//trip.setMyTripDayList(dayList);
		return trip;
	}
	/**
	 * 我的行程首页内容
	 * @param request
	 * @throws AppBusinessException
	 */
	@Override
	public void getIndexTrip(HttpServletRequest request)throws AppBusinessException {
		
		
	}
}
